<%--
  Created by IntelliJ IDEA.
  User: lujw
  Date: 2017/6/8
  Time: 12:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/hospital_token.jsp"%>
<%!
    /**
     * 修改密码
     * @param token
     * @param parameters
     * @return
     */
    public Message password(HospitalToken token, Map<String, String> parameters) {
        Message msg = new Message();
        List<HospitalToken> list = QBuilder.db("basedb").queryHql("from HospitalToken where hospital = "+ token.getHospital() +" and token = '" + token.getToken() + "'");
        if (list.size() == 0) {
            ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_USER);
            return msg;
        }
        String oldPassword = parameters.get("old_password");
        if (Objects.isNull(oldPassword) || !oldPassword.equals(token.getPassword())) {
            ErrcodeUtil.setErrcode(msg, ErrcodeUtil.WRONG_PASSWORD);
            return msg;
        }
        String newPassword = parameters.get("password");
        if (oldPassword.equals(newPassword)) {
            msg.setErrcodeAndErrmsg(30999, "新密码与原密码相同");
            return msg;
        }
        token = list.get(0);
        token.setPassword(newPassword);
        // 修改token中的密码
        DBTools.db("basedb").modItem(token, token.getId());
        List<HospitalUser> hospitalUsers = QBuilder.db("basedb").select().from("HospitalUser").where(new Where("id=", token.getUid()).and("status=", ConstantUtil.STATUS_ENABLE)).query().getResultObj();
        if (hospitalUsers.size() == 0) {
            ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_USER);
            return msg;
        }
        HospitalUser hospitalUser = hospitalUsers.get(0);
        hospitalUser.setPassword(newPassword);
        DBTools.db("basedb").modItem(hospitalUser, hospitalUser.getId());
        // 修改密码后删除其他设备登录的token
        QBuilder.db("basedb").execHQL("delete from token where userName = '" + token.getUserName() + "' hospital = "+ token.getHospital() +" and uid = " + token.getUid() + " and token !='" + token.getToken() + "'");
        msg.setErrmsg("修改成功");
        return msg;
    }
%>
<%
    Tools.encryptPrint(out, password(token, parameters));
%>
